package com.xxkj.nmip.page;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;
import com.xxkj.nmip.js.JsApi;


public abstract class BaseActivity extends AppCompatActivity  {
    private Handler handler;
    private JsApi.OnactivityRelsultLister onactivityRelsultLister;
    private JsApi.OnactivityRestartLister onactivityRestartLister;
    public Context mContext;
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ImmersionBar.with(this).hideBar(BarHide.FLAG_HIDE_BAR).init();
        handler = new Handler();
        mContext = this;

    }

    public void setOnactivityRestartLister(JsApi.OnactivityRestartLister onactivityRestartLister) {
        this.onactivityRestartLister = onactivityRestartLister;
    }

    public void setOnactivityRelsultLister(JsApi.OnactivityRelsultLister onactivityRelsultLister) {
        this.onactivityRelsultLister = onactivityRelsultLister;
    }

    public void unActivityRelsultLister() {
        this.onactivityRelsultLister = null;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (onactivityRelsultLister != null) {
            onactivityRelsultLister.onActivityResult(requestCode, resultCode, data);
        }
    }

    /**
     * 重写 getResource 方法，防止系统字体影响
     */
    @Override
    public Resources getResources() {//禁止app字体大小跟随系统字体大小调节
        Resources resources = super.getResources();
        if (resources != null && resources.getConfiguration().fontScale != 1.0f) {
            android.content.res.Configuration configuration = resources.getConfiguration();
            configuration.fontScale = 1.0f;
            resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        }
        return resources;
    }

    @Override
    protected void onRestart() {
        super.onRestart();

        if (onactivityRestartLister != null) {
            onactivityRestartLister.onActivityRestart();
        }
    }

    private void hideNavigarionBar() {
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }

    }
}
